package com.example.drainage.ui.realmdao

import com.example.arm.mvvm.BaseViewModel
import io.realm.RealmList

/**
 *  author : yanghaozhang
 *  date : 2022/8/3 11:43
 *  description :
 */
class RealmDaoViewModel : BaseViewModel() {

    var incrementInt = 0
        get() {
            field++
            return field
        }

    var incrementString = "0"
        get() {
            return incrementInt.toString()
        }

    fun testAdd() {
        RealmDaoController.savePackageBean(createPackageBean())
        soutMsg()
    }

    /**
     * 删除后可以看到RealmDaoPackageBean剩余个数为1,RealmDaoObj剩余个数为4
     * 说明删除时不删除关联的对象
     *
     */
    fun testDelete() {
        val bean = createPackageBean()
        RealmDaoController.savePackageBean(bean)
        RealmDaoController.savePackageBean(createPackageBean())
        RealmDaoController.deletePackageBean(bean.id)
        soutMsg()
    }

    fun testDeleteAll() {
        RealmDaoController.savePackageBean(createPackageBean())
        RealmDaoController.savePackageBean(createPackageBean())
        RealmDaoController.deletePackageBean()
        RealmDaoController.deleteObjBean()
        soutMsg()
    }

    private fun soutMsg() {
        val message = RealmDaoController.getPackageBean()
        val message1 = RealmDaoController.getObjBean()
        println(message)
        println(message.size)
        println(message1)
        println(message1.size)
    }

    fun createPackageBean(): RealmDaoPackageBean {
        val list = RealmList(createObjBean())
        return RealmDaoPackageBean(createObjBean(), list)
    }

    fun createObjBean(): RealmDaoObj {
        return RealmDaoObj(
            "String:${incrementString}",
            incrementInt,
            incrementInt.toFloat(),
            incrementInt.toDouble(),
            null
        )
    }

    fun getRandomInt(): Int {
        return (Math.random() * 10).toInt() / 3
    }
}